package com.yang.myoschina.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.thoughtworks.xstream.XStream;
import com.yang.myoschina.R;
import com.yang.myoschina.adapter.NewsAdapter;
import com.yang.myoschina.api.remote.OsChina;
import com.yang.myoschina.bean.News;
import com.yang.myoschina.bean.NewsList;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends AppCompatActivity{

    private ListView listView;
    private NewsAdapter newsAdapter;
    private NewsList newsList;
    private int pageNow = 1;
    private int totalPage =10;
    private AsyncHttpResponseHandler mNewsHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("OsChina新闻");
        //测试按钮
        //button = (Button)findViewById(R.id.testButton);
        // button.setOnClickListener(this);

        /*mNewsHandler = new NewsHttpHandler();
        listView = (ListView)findViewById(R.id.newsListView);
        OsChina.getNewsList(1,pageNow,mNewsHandler);
        newsList = new NewsList();
        newsAdapter = new NewsAdapter(MainActivity.this,R.layout.news_listview,newsList.getList());
        listView.setAdapter(newsAdapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                int lastVp = view.getLastVisiblePosition();
                if ((lastVp+1)==newsList.getList().size()){
                    //加载数据
                    if (totalPage>pageNow){
                        pageNow = pageNow+1;
                        Log.d("MyTag","开始加载数据，页数："+pageNow);
                        OsChina.getNewsList(1,pageNow,mNewsHandler);
                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                //Log.d("MyTag","firstVisibleItem:"+firstVisibleItem+",visibleItemCount:"+visibleItemCount+",totalItemCount:"+totalItemCount);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = newsList.getList().get(position);
                Log.d("MyTag","news:"+news.getId()+",title:"+news.getTitle());
                Intent intent = new Intent(MainActivity.this,NewsDetailActivity.class);
                intent.putExtra("NewsId",news.getId());
                startActivity(intent);
            }
        });*/

        Intent i = new Intent(MainActivity.this,TestUIActivity.class);
        startActivity(i);
    }

    class NewsHttpHandler extends AsyncHttpResponseHandler {
        XStream xStream = new XStream();
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            String resp = new String(responseBody);
            Log.d("MyTag",resp);
            xStream.processAnnotations(News.class);
            xStream.processAnnotations(NewsList.class);
            NewsList respNewsList = (NewsList)xStream.fromXML(resp);
            newsList.getList().addAll(respNewsList.getList());
            newsAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

        }
    }
}
